//
//  HJHVisitorView.swift
//  WeiBoPro
//
//  Created by hejinhui on 2018/5/9.
//  Copyright © 2018年 hjh. All rights reserved.
//

import UIKit

@objc protocol LoginBtnDelegate:NSObjectProtocol {
    @objc optional func  loginBtnClick(tag:Int)
}

class HJHVisitorView: UIView {
    var Loginblock:((_ tag: Int) -> ())?
    var delegate:LoginBtnDelegate?
    
    //访客试图的内容
    var visitorInfo:[String:String]? {
        didSet{
            guard  let imageName = visitorInfo?["imageName"],let imageMsg = visitorInfo?["imageMsg"]
                else {
                    return
                }
            //首页不需要设置
            if imageName == "" {
                startAnimation()
                return
            }
            self.cicleImageV.isHidden = true
            self.houseImageV.image = UIImage(named: imageName)
            self.tipLabel.text = imageMsg
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        setContraints()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func loginBtnClick(sender:UIButton)
    {
        if Loginblock != nil {
            Loginblock!(sender.tag)
        }
        
        if (self.delegate != nil) {
            self.delegate?.loginBtnClick!(tag: sender.tag)
        }
    }
    //旋转动画
    private func startAnimation()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        anim.isRemovedOnCompletion = false
        cicleImageV.layer.add(anim, forKey: nil)
    }
    
  private  lazy var cicleImageV: UIImageView = {
        let imgv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imgv
    }()
  private  lazy var  houseImageV: UIImageView = {
        let imgv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imgv
    }()
  private  lazy var tipLabel: UILabel = {
        let lab = UILabel()
        lab.text = "登陆后才能看到数据哟,登陆后才能看到数据哟"
        lab.numberOfLines = 0
        lab.textColor = UIColor.gray
        return lab
    }()
   private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", for: .normal)
        btn.backgroundColor = UIColor.red
        btn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        btn.tag = 0
        return btn
    }()
  private  lazy var registBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.backgroundColor = UIColor.orange
        btn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        btn.tag = 1
        return btn
    }()
    
}
extension HJHVisitorView
{
    func setUI() {
        addSubview(cicleImageV)
        addSubview(houseImageV)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registBtn)

    }
    func setContraints() {
        
        cicleImageV.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-50)
        }
        houseImageV.snp.makeConstraints { (make) in
            make.center.equalTo(cicleImageV)
        }
        tipLabel.snp.makeConstraints { (make) in
        
            make.top.equalTo(cicleImageV.snp.bottom).offset(20)
            make.width.equalTo(236)
            make.centerX.equalTo(self.snp.centerX)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(40)
            make.right.equalTo(self.snp.centerX).offset(-10)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
        }
        registBtn.snp.makeConstraints { (make) in
            make.width.height.top.equalTo(loginBtn)
            make.left.equalTo(self.snp.centerX).offset(10)
        }
    }
}


